$(function () {
    var pathName=window.document.location.pathname;
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    //新增Groups--选择分组下拉框
    $("#groupsSelect_1").mousedown(function () {
        var groupsSelect_1Obj = $("#groupsSelect_1");
        var groupsSelect_1Offset = $("#groupsSelect_1").offset();
        $("#groupsTreeAdd_1").css({
            left: groupsSelect_1Offset.left + "px",
            top: groupsSelect_1Offset.top + groupsSelect_1Obj.outerHeight() + "px"
        }).slideDown("fast");

    });
    //点击其他地方隐藏分组下拉框
    $("body").mousedown(function (event) {
        if (!(event.target.id == "groupsSelect_1" || $(event.target).parents("#groupsTreeAdd_1").length > 0)) {
            groups.hideGroupsTree();
        }
    });
    //初始化选择的树
    var setting = {
        async: {
            enable: true,
            url: projectName+"/groups/getTree",
            autoParam: ["id"]
        },
        data: {
            key: {
                name: "groupName",
            }
        },
        callback: {
            onClick: groups.onClickGroups,
        }
    };
    zTreeObj = $.fn.zTree.init($("#groupsTree"), setting);
    instance = zTreeObj;

    //关闭对话框
    $("#addCancel").click(function () {
        groups.close();
    });
    //提交新建分组
    $("#addEnsure").click(function () {
        groups.addSubmit();
    });
    //提交编辑分组
    $("#editEnsure").click(function () {
        groups.editSubmit();
    });
});

var groups = {
    groupsData:{},
    tipTime : 1500,
}

//点击分组树，填输入框
groups.onClickGroups = function (e, treeId, treeNode) {
    $("#groupsSelect_1").attr("value", treeNode.groupName);
    $("#pId").attr("value", treeNode.id);
    groups.hideGroupsTree();
}
//隐藏选择分组的树
groups.hideGroupsTree = function () {
    $("#groupsTreeAdd_1").fadeOut("fast");
};
//关闭此对话框
groups.close = function () {
    parent.layer.close(window.parent.Groups.layerIndex);
}
//提交新建分组
groups.addSubmit = function () {
    this.clearData();
    this.collectData();
    var pathName=window.document.location.pathname;
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    $.ajax({
        type:'post',
        contentType:'application/json',
        dataType:'json',
        url:projectName+'/groups/addOne',
        data:JSON.stringify(this.groupsData),
        success:function (data) {
            if(data.success){
                groups.close();
                Feng.success(data.msg||"添加成功!");
            } else {
                Feng.error(data.msg||"添加失败!");
            }
        }
    })
}
//提交编辑分组
groups.editSubmit = function () {
    this.clearData();
    this.collectData();
    var pathName=window.document.location.pathname;
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    $.ajax({
        type:'post',
        contentType:'application/json',
        dataType:'json',
        url:projectName+'/groups/editOne',
        data:JSON.stringify(this.groupsData),
        success:function (data) {
            if(data.success){
                groups.close();
                Feng.success(data.msg||"修改成功!");
            } else {
                Feng.error(data.msg||"修改失败!");
            }
        }
    })
}
//清除数据
groups.clearData = function () {
    this.groupsData = {};
}
//收集数据
groups.collectData = function () {
    this.set('groupsName','groupName').set('pId').set("groupsId","id");
}
//收集数据--按照id,第一个参数是input的id，第二个若没有第一个参数必须是实体的属性名，若有则为实体的属性名，即input的id可以随意起名
groups.set = function (key,modelKey,value) {
    if(typeof value == "undefined"){
        if(typeof $("#" + key).val() =="undefined"){
            var str="";
            var ids="";
            $("input[name='"+key+"']:checkbox").each(function(){
                if(true == $(this).is(':checked')){
                    str+=$(this).val()+",";
                }
            });
            if(str){
                if(str.substr(str.length-1)== ','){
                    ids = str.substr(0,str.length-1);
                }
            }else{
                $("input[name='"+key+"']:radio").each(function(){
                    if(true == $(this).is(':checked')){
                        ids=$(this).val()
                    }
                });
            }
            if(typeof modelKey == "undefined"){
                this.groupsData[key] = ids;
            }else {
                this.groupsData[modelKey] = ids;
            }
        }else{
            if(typeof modelKey == "undefined"){
                this.groupsData[key]= $("#" + key).val();
            }else {
                this.groupsData[modelKey] = $("#" + key).val();
            }
        }
    }
    return this;
}
